#coding:utf-8
'''
create on 2017-06-09

@author:sandy
'''

import time
import logging
import GameFactory
import GateFactory
import CenterBuild
import GameDb
from pycommon import Const
from pycommon import LogServer

INIT_SID = -1

class User(object):
    def __init__(self,uid,pid,session):
        self.uid = uid
        self.gateClientPid = pid
        self.gameClientPid = INIT_SID
        self.session = session
        self.gatesid = INIT_SID
        self.gamesid = INIT_SID
        
        self.lastactivetime = 0
        self.begingametime = 0
        self.nickname = u''
        self.photo = u''
        self.sex = 0
        self.cver = 0 #版本
        self.partner = 0 #平台

        self.propMap = {} #道具
        self.propertyMap = {}

    def setUserBasicInfo(self, gatesid, cver, partner, nickname, photo, sex):
        self.gatesid = gatesid
        self.nickname = nickname
        self.photo = photo
        self.sex = sex
        self.cver = cver
        self.partner = partner

    def changeSession(self, session):
        self.session = session

    def setGateClientPid(self,pid):
        self.gateClientPid = pid

    def setGameClientPid(self,pid):
        self.gameClientPid = pid
    
    def getUid(self):
        return self.uid

    def setSession(self,session):
        self.session = session

    def getSession(self):
        return self.session

    def setGameSid(self,sid):
        self.gamesid = sid

    def getGameSid(self):
        return self.gamesid

    def getGateSid(self):
        return self.gatesid
    
    def setGateSid(self, sid):
        self.gatesid = sid

    def setLastActiveTime(self):
        self.lastactivetime = time.time()

    def setBeginGameTime(self):
        self.begingametime = time.time()
        
    def getBeginGameTime(self):
        return self.begingametime
    

    def checkUserSuc(self,gamesid,session,pid):
        logging.debug(u"$$$$$$$$$$$$$$$$$$$$$$$$$$$checkUserSuc$$$$$$$$$$$$$$$$$$$$$$$$$$")
        self.setGameSid(gamesid)
        self.setGameClientPid(pid)
        gameSrv = GameFactory.instance.getGameSrvBySid(gamesid)
        if gameSrv == None:
            logging.error(u"checkUserSuc(), self.gameserver == None, uid=%d", self.uid)
            return
        logging.debug(u"checkUserSuc(), uid=%d", self.uid)
        gameSrv.sendPacket(CenterBuild.checkUser(Const.ERR_OK,self.uid,self.gameClientPid,session,self.nickname,self.photo,self.sex))
        #同步玩家背包 道具等等数据
        gameSrv.sendPacket(CenterBuild.sendPropToGame(self.uid,self.propMap))
        gameSrv.sendPacket(CenterBuild.sendPropertyToGame(self.uid,self.propertyMap))
        gameSrv.sendPacket(CenterBuild.sendInfoEndToGame(self.uid))
        pass

    def kickFromGame(self,code):
        logging.debug(u"kickFromGame uid=%d", self.uid)
        gameSrv = GameFactory.instance.getGameSrvBySid(self.gamesid)
        self.gamesid = INIT_SID
        if gameSrv == None:
            logging.error(u"kick(), self.gameserver == None, uid=%d", self.uid)
            return
        gameSrv.sendPacket(CenterBuild.kickUserFromGame(self.uid,self.gameClientPid,code))


    def kickFromGate(self,code):
        logging.debug(u"kickFromGate uid=%d gateClientPid=%d", self.uid,self.gateClientPid)
        gateSrv = GateFactory.instance.getGateSrvBySid(self.gatesid)
        self.gatesid = INIT_SID
        if gateSrv == None:
            logging.error(u"kick(), self.gateserver == None, uid=%d", self.uid)
            return
        gateSrv.sendPacket(CenterBuild.kickUserFromGate(self.uid,self.gateClientPid,code))


    def loadProp(self,propList):
        self.propMap.clear()
        for row in propList:
            pid = row[0]
            pvalue = row[1]
            self.propMap[pid] = pvalue
            logging.debug(u"uid=%d loadProp pid=%d pvalue=%d",self.uid,pid,pvalue)
        pass
    
    def loadProperty(self,property):
        self.propertyMap.clear()
        for row in property:
            pid = row[0]
            pvalue = row[1]
            self.propertyMap[pid] = pvalue
            logging.debug(u"uid=%d loadProperty pid=%d pvalue=%d",self.uid,pid,pvalue)
        pass

    def loadUserInfoFinished(self):
        if self.partner == 0:
            self.sendBaseInfoToClient()
        self.sendPropToClient()
        self.sendPropertyToClient()
        self.sendDataEndToClient()
        pass
    

    def sendBaseInfoToGame(self):
        if self.gamesid != INIT_SID:
            gameSrv = GameFactory.instance.getGameSrvBySid(self.gamesid)
            if gameSrv != None:
                gateSrv.sendPacket(CenterBuild.sendBaseInfoToGame(self.uid,self.nickname,self.photo,self.sex))
            else:
                logging.debug(u"sendBaseInfoToGame gameId=%d and gameSrv==None",self.gamesid)
        pass

    def sendBaseInfoToClient(self):
        if self.gatesid != INIT_SID:
            gate = GateFactory.instance.getGateSrvBySid(self.gatesid)
            if gate != None:
                gate.transPkt2Client(self.uid,CenterBuild.sendBaseInfoToClient(self.uid,self.nickname,self.photo,self.sex))
            else:
                logging.debug(u"sendBaseInfoToClient gateId=%d and gate==None",self.gatesid)
        pass

    def sendPropToGame(self):
        if self.gamesid != INIT_SID:
            gameSrv = GameFactory.instance.getGameSrvBySid(self.gamesid)
            if gameSrv != None:
                gateSrv.sendPacket(CenterBuild.sendPropToGame(self.uid,self.propMap))
            else:
                logging.debug(u"sendPropToGame gameId=%d and gameSrv==None",self.gamesid)
        pass

    def sendPropToClient(self):
        if self.gatesid != INIT_SID:
            gate = GateFactory.instance.getGateSrvBySid(self.gatesid)
            if gate != None:
                gate.transPkt2Client(self.uid,CenterBuild.sendPropToClient(self.propMap))
            else:
                logging.debug(u"sendPropToClient gateId=%d and gate==None",self.gatesid)
        pass

    def sendPropertyToGame(self):
        if self.gamesid != INIT_SID:
            gameSrv = GameFactory.instance.getGameSrvBySid(self.gamesid)
            if gameSrv != None:
                gameSrv.sendPacket(CenterBuild.sendPropertyToGame(self.uid,self.propertyMap))
            else:
                logging.debug(u"sendPropToGame gameId=%d and gameSrv==None",self.gamesid)
        pass

    def sendPropertyToClient(self):
        if self.gatesid != INIT_SID:
            gate = GateFactory.instance.getGateSrvBySid(self.gatesid)
            if gate != None:
                gate.transPkt2Client(self.uid,CenterBuild.sendPropertyToClient(self.propertyMap))
            else:
                logging.debug(u"sendPropertyToClient gateId=%d and gate==None",self.gatesid)
        pass

    def sendDataEndToClient(self):
        if self.gatesid != INIT_SID:
            gate = GateFactory.instance.getGateSrvBySid(self.gatesid)
            if gate != None:
                gate.transPkt2Client(self.uid,CenterBuild.loginSucess(Const.ERR_OK))
            else:
                logging.debug(u"sendDataEndToClient gateId=%d and gate==None",self.gatesid)
        pass


    def getPropById(self,propId):
        return self.propMap.get(propId,0)

    def setPropCount(self,propId,count):
        self.propMap[propId] = count

    def addProp(self,propId,propCount):
        count = self.getPropById(propId)
        self.setPropCount(count + propCount)


    def userLoginGame(self,gamesid):
        self.setBeginGameTime()
        self.setGameSid(gamesid)
        self.setLastActiveTime()
        pass

    def userLogoutGame():
        self.setGameSid(0)
        gametime = int(time.time() - self.getBeginGameTime())
        logging.debug(u"uid=%d userLogoutGame and inGameTime = %d",self.uid,gametime)
        pass

    def chargeSucess(self,needValueItems,way = Const.PROP_CHANGE_DEFAUTL):
        itemList = Utils.parseValueItems(needValueItems)
        for item in itemList:
            propKind = item[0]
            propId = int(item[1])
            propCount = int(item[2])
            logging.debug(u"updateProp propKind=%s propId=%d propCount=%d way=%d",propKind,propId,propCount,way)
            if propKind == u'P':
                self.addProp(propId,propCount)
                self.sendPropToClient()
            else:
                logging.error("updateProp propKind error")

    def useProp(self,propId,propCount,way):
        count = self.getPropById(propId)
        if count > propCount:
            self.propMap[propId] = count - propCount
            GameDb.instance.updateProp(self.uid,propId,-propCount,way)
        else:
            logging.debug("uid=%d decProp error propId=%d hasCount=%d decCount=%d",self.uid,propId,count,propCount)
            

if __name__ == "__main__":
    pass